#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Swap(int* x1, int* x2)
{
	int k = *x1;
	*x1 = *x2;
	*x2 = k;
}

void Print(int* p, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", p[i]);
	}
	printf("\n");
}

void SelectSort(int* p, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int min = begin;
		int max = end;
		for (int i = begin; i <= end; i++)
		{
			if (p[i] < p[min])
			{
				min = i;
			}
			if (p[i] > p[max])
			{
				max = i;
			}
			if (max == end)
			{
				max = min;
			}
		}
		Swap(&p[min], &p[end]);
		Swap(&p[max], &p[begin]);
		begin++;
		end--;
	}
}

void BubbleSort(int* p, int n)
{
	for (int x = 0; x < n - 1; x++)
	{
		int exchange = 0;
		for (int y = 0; y < n - x - 1; y++)
		{
			if (p[y] > p[y + 1])
			{
				Swap(&p[y], &p[y + 1]);
				exchange = 1;
			}
		}
		if (exchange == 0)
		{
			break;
		}
	}
}

int GetMidIndex(int* p, int left, int right)
{
	int mid = (left + right) >> 1;
	if (p[left] < p[right])
	{
		if (p[left] > p[mid])
		{
			return left;
		}
		else if (p[mid] > p[right])
		{
			return right;
		}
		else
		{
			return mid;
		}
	}
	else
	{
		if (p[left] < p[mid])
		{
			return left;
		}
		else if (p[mid] < p[right])
		{
			return right;
		}
		else
		{
			return mid;
		}
	}
}

//void QuickSort(int* p, int left, int right)
//{
//	if (left >= right)
//	{
//		return;
//	}
//	int index = GetMidIndex(p, left, right);
//	Swap(&p[left], &p[index]);
//	int begin = left;
//	int end = right;
//	int key = p[left];
//	int cur = left;
//	while (begin < end)
//	{
//		while (begin < end && p[end] >= key)
//		{
//			end--;
//		}
//		Swap(&p[end], &p[cur]);
//		cur = end;
//		while (begin < end && p[begin] <= key)
//		{
//			begin++;
//		}
//		Swap(&p[begin], &p[cur]);
//		cur = begin;
//	}
//	QuickSort(p, left, begin - 1);
//	QuickSort(p, end + 1, right);
//}

//int PartSort(int* p, int left, int right)
//{
//	int index = GetMidIndex(p, left, right);
//	Swap(&p[left], &p[index]);
//	int begin = left;
//	int end = right;
//	int key = p[left];
//	while (begin < end)
//	{
//		while (begin < end && p[end] >= key)
//		{
//			end--;
//		}
//		while (begin < end && p[begin] <= key)
//		{
//			begin++;
//		}
//		Swap(&p[begin], &p[end]);
//	}
//	Swap(&p[begin], &p[left]);
//	return begin;
//}

int PartSort(int* p, int left, int right)
{
	int index = GetMidIndex(p, left, right);
	Swap(&p[left], &p[index]);
	int begin1 = left;
	int begin2 = left + 1;
	int key = p[left];
	while (begin2 <= right)
	{
		if (p[begin2] < key)
		{
			begin1++;
			Swap(&p[begin2], &p[begin1]);
		}
		begin2++;
	}
	Swap(&p[begin1], &p[left]);
	return begin1;
}

void QuickSort(int* p, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int index = PartSort(p, left, right);
	QuickSort(p, left, index - 1);
	QuickSort(p, index + 1, right);
}

void InsertSort(int* p, int n)
{
	int i = 0;
	while (i < n - 1)
	{
		int end = i;
		int tmp = p[end + 1];
		while (end >= 0)
		{
			if (tmp < p[end])
			{
				p[end + 1] = p[end];
				end--;
			}
			else
			{
				break;
			}
			p[end + 1] = tmp;
		}
		i++;
	}
}

void ShellSort(int* p, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = p[i + gap];
			while (end >= 0)
			{
				if (tmp > p[end])
				{
					p[end + gap] = p[end];
					end -= gap;
				}
				else
				{
					break;
				}
				p[end + gap] = tmp;
			}
		}
	}
}

void AdjustDoen(int* p, int n, int root)
{
	int parent = root;
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && p[child] < p[child + 1])
		{
			child += 1;
		}
		if (p[parent] < p[child])
		{
			Swap(&p[parent], &p[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* p, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDoen(p, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&p[end], &p[0]);
		AdjustDoen(p, end, 0);
		end--;
	}
}

void _MergeSort(int* p, int left, int right, int* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) >> 1;
	_MergeSort(p, 0, mid, tmp);
	_MergeSort(p, 0, mid + 1, tmp);
	int begin1 = left;
	int begin2 = mid;
	int index = left;
	while (begin1 < mid && begin2 < right)
	{
		if (p[begin1] < p[begin2])
		{
			tmp[index++] = p[begin1++];
		}
		else
		{
			tmp[index++] = p[begin2++];
		}
	}
	while (begin1 < mid)
	{
		tmp[index++] = p[begin1++];
	}
	while (begin2 < right)
	{
		tmp[index++] = p[begin2++];
	}
	for (int i = left; i <= right; i++)
	{
		p[i] = tmp[i];
	}
}

void MergeSort(int* p, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	_MergeSort(p, 0, n - 1, tmp);
	free(tmp);
}

int main()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1 };
	QuickSort(arr, 0, 8);
	Print(arr, 9);
	SelectSort(arr, 9);
	Print(arr, 9);
	InsertSort(arr, 9);
	Print(arr, 9);
	ShellSort(arr, 9);
	Print(arr, 9);
	return 0;
}